package com.bitcola.security.activity.biz;


import com.bitcola.security.activity.constant.AirdropConstant;
import com.bitcola.security.activity.constant.PrizeConstant;
import com.bitcola.entity.Airdrop;
import com.bitcola.security.activity.mapper.AirdopMapper;
import com.bitcola.security.activity.mapper.BalanceChangeLogMapper;
import com.bitcola.security.activity.mapper.ColaUserBalanceMapper;
import com.bitcola.security.auth.common.util.EncoderUtil;
import com.bitcola.security.common.constant.BalanceTypeConstants;
import com.bitcola.security.common.constant.FinancialConstant;
import com.bitcola.security.common.exception.RepeatException;
import com.bitcola.security.common.util.Snowflake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class Airdropbiz {
    @Autowired
    AirdopMapper airdopMapper;

    @Autowired
    BalanceChangeLogMapper balanceChangeLogMapper;
    @Autowired
    ColaUserBalanceMapper colaUserBalanceMapper;
    Snowflake snowflake=new Snowflake(2,11);

    @Autowired
    BalanceChangeBiz balanceChangeBiz;
    @Transactional
    public void withdrawAirdrop(String id,String userId){
        int index= airdopMapper.updateStatus(AirdropConstant.Pending,AirdropConstant.Finish,id);
        if(index==0) throw  new RepeatException("已经领取过了");
        var airdrop=airdopMapper.selectByPrimaryKey(id);
        if(airdrop.getUserId().equals(userId)){
            var log= balanceChangeBiz.createChange(userId, BalanceTypeConstants.BALANCE, PrizeConstant.AIRDROP,airdrop.getAmount(), FinancialConstant.ACTIVITY_REWARD,airdrop.getCoinCode());
            colaUserBalanceMapper.changeUserBanlance(userId,airdrop.getCoinCode(),airdrop.getAmount(), BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO, EncoderUtil.BALANCE_KEY);

        }


    }


    public List<Airdrop>history(String userid,int page,int size){

        return airdopMapper.history(userid,size,(page-1)*size);


    }

    public void apply(String userId,String coinCode,BigDecimal amount){


        Airdrop airdrop=new Airdrop();
        airdrop.setId(snowflake.nextIdStr());
        airdrop.setAmount(amount);
        airdrop.setCoinCode(coinCode);
        airdrop.setCreateTime(new Date());
        airdrop.setStatus(AirdropConstant.Pending);
        airdrop.setUserId(userId);
        airdopMapper.insert(airdrop);
    }






}
